// //设置cookie用例**********
// options{
//     key//key为cookie的键
//     val//val为cookie的值
//     days//days为有效天数
//     path//path为设置的路径
//     domain//domain为设置的域名
// }
//设置cookie用例**********
// 设置cookie
var setCookie = function setCookie(options) {
    if (!options.key || !options.val) {
        // return
        throw new Error('key和val是必传参数')
    }
    var days = options.days || 0
    var path = options.path || ''
    var domain = options.domain || ''
    // escape(options.val) 对存储的数据进行编码处理
    if (days === 0) { //不设置有效期
        document.cookie = options.key + '=' + escape(options.val) + '; path=' + path + '; domain=' + domain
    } else {
        var d = new Date()
        d.setDate(d.getDate() + days)
        document.cookie = options.key + '=' + escape(options.val) + '; path=' + path + '; domain=' + domain + '; expires=' + d
    }
}

// 获取cookie
var getCookie = function getCookie(key) {
    var arr1 = document.cookie.split('; ')
    //["user1=111asdasd", "user2=222gfhft", "user3=333wwwaa"]
    for (var i = 0; i < arr1.length; i++) {
        var arr2 = arr1[i].split('=') //['user1','111asdasd']
        if (arr2[0] === key) {
            // 对数据进行解码并返回
            return unescape(arr2[1])
        }
    }
    return null
}

// 删除cookie
var removeCookie = function removeCookie(key) {
    setCookie({
        key: key,
        val: '123',
        days: -2
    })
}
export {
    removeCookie,
    getCookie,
    setCookie
}